package com.youhome.service.utils;

/**
 * Created by chen365 on 2018/08/22.
 */
public class GPolyline2 {
    /**
     * 处理有符号整数
     *
     * @param point
     * @return
     */
    private static String encodeSignedNumber(int point) {
        // 对二进制低位补0
        int _point_int = point << 1;
        // 如果原来的数是负数则求反，取其补码
        if (point < 0) {
            _point_int = ~_point_int;
        }
        return (encodeNumber(_point_int));
    }
    /**
     * 处理无符号整数
     *
     * @param num
     * @return
     */
    private static String encodeNumber(int num) {
        String resultString = "";
        while (num >= 0x20) {
            int _block = num & 0x1F;
            _block = (_block | 0x20) + 63;
            char _result = (char) _block;
            resultString += _result;
            num >>= 5;
        }
        resultString += (char) (num + 63);
        return resultString;
    }
    /**
     * 计算编码级别
     *
     * @param line
     * @return
     */
    public static String encodeLevel(String line) {
        int level = 3;// 设置编码级别
        String encodeLel = encodeNumber(level);// 计算出编码级别，因为预先固定，考虑效率问题，只计算一次
        StringBuffer result = new StringBuffer();
        int length = line.split(";").length;// 得到点个数
        for (int i = 0; i < length; i++) {
            result.append(encodeLel);
        }
        // System.out.println("2level: " + result.toString());
        return result.toString();
    }
    /**
     * 计算编码折线
     *
     * @param line
     * @return
     */
    public static String createEncodings(String line) {
        String[] _points = null;
        String _result = "";
        int plat = 0;
        int plng = 0;
        _points = line.split("\\|");// 每个经纬度坐标值以";"分隔
        for (String _point : _points) {
            String[] _latlng = _point.split(",");// 经纬度值之间以","分隔
            double lat = Double.parseDouble(_latlng[0]);
            double lng = Double.parseDouble(_latlng[1]);
            int late5 = (int) Math.floor(lat * 1e5);
            int lnge5 = (int) Math.floor(lng * 1e5);
            int dlat = late5 - plat;
            int dlng = lnge5 - plng;
            plat = late5;
            plng = lnge5;
            _result += encodeSignedNumber(dlat) + encodeSignedNumber(dlng);
        }
        // System.out.println("2bianma--->" + _result);
        return _result;
    }
//    public static void main(String[] args) {
//        String line = "40.751617,-74.00243";
//        System.out.println(createEncodings(line));
//    }
}
